<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="css/reset.css">
    <link rel="stylesheet" href="css/iconfont.css">
    <link rel="stylesheet" href="css/main.css">
    <script src="./libs/jquery-1.12.4.js"></script>
    <script src="./libs/bootstrap/js/bootstrap.min.js"></script>
    <!-- 引入模板引擎js文件 -->
    <script src="./libs/template-web.js"></script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            文章类别管理
        </div>
        <div class="container-fluid common_con">
            <table class="table table-striped table-bordered table-hover mp20 category_table">
                <thead>
                    <tr>
                        <th>名称</th>
                        <th>Slug</th>
                        <th class="text-center" width="100">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>奇趣事</td>
                        <td>funny</td>
                        <td class="text-center">
                            <a href="javascript:void(0)" data-toggle="modal" data-target="#myModal"
                                class=" btn btn-info btn-xs">编辑</a>
                            <a href="javascript:void(0)" class="btn btn-danger btn-xs">删除</a>
                        </td>
                    </tr>
                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="3" class="text-center">
                            <a href="javascript:void(0)" class="btn btn-success" id="xinzengfenlei" data-toggle="modal"
                                data-target="#myModal">新增分类</a>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>


    <!-- bootstrap模态框 -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="exampleModalLabel">New message</h4>
                </div>
                <div class="modal-body">
                    <form>
                        <input type="hidden" id="categoryID" name="id">
                        <div class="form-group">
                            <label for="recipient-name" class="control-label">类别:</label>
                            <input type="text" name="name" class="form-control" id="recipient-name">
                        </div>
                        <div class="form-group">
                            <label for="message-text" class="control-label">类别名称:</label>
                            <input type="text" name="slug" class="form-control" id="message-text"></input>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" id="addOredit">Send message</button>
                </div>
            </div>
        </div>
    </div>

</body>
<script id="temp" type="text/html">
    {{each data v}}
    <tr>
        <td>{{v.name}}</td>
        <td>{{v.slug}}</td>
        <td class="text-center">
            <a href="javascript:void(0)" data-id="{{v.id}}" data-toggle="modal" data-target="#myModal" class=" btn btn-info btn-xs">编辑</a>
            <a href="javascript:void(0)" data-id="{{v.id}}" class="btn btn-danger btn-xs" id="delete">删除</a>
        </td>
    </tr>
    {{/each}}
</script>
<script src="./libs/http.js"></script>
<script>
    //入口函数
    $(function () {
        //发送Ajax请求，将默认数据展示在页面上
        function gitData() {
            $.ajax({
                type: 'get',
                url: 'http://localhost:8080/api/v1/admin/category/list',
                success: function (backData) {
                    if (backData.code == 200) {
                        let res = template('temp', backData)
                        $('tbody').html(res)
                    }
                }
            })
        }
        gitData()

        //调用bootstrap事件
        $('#myModal').on('show.bs.modal', function (e) {
            // do something...
            let res = e.relatedTarget

            //判断点击的是新增分类还是编辑按钮
            if ($(res).text() == '新增分类') {
                $('#exampleModalLabel').text('新增分类')
                $('#addOredit').text('新增').addClass('btn-primary').removeClass('btn-success')
            } else {
                $('#exampleModalLabel').text('编辑')
                $('#addOredit').text('编辑').addClass('btn-success').removeClass('btn-primary')

                //编辑第一步
                //获取到当前的分类信息，并且添加到模态框中
                $('#recipient-name').val($(res).parent().prev().prev().text().trim())
                $('#message-text').val($(res).parent().prev().text().trim())
                $('#categoryID').val($(res).attr('data-id'))
            }
        })

        // 给模态框中的新增/编辑按钮设置点击事件
        $('#addOredit').on('click', function () {
            if ($(this).hasClass('btn-primary')) {
                //获取要新增的类别以及类别名称
                let categoryname = $('#recipient-name').val().trim()
                let categoryslug = $('#message-text').val().trim()
                if (categoryname == '' || categoryslug == '') {
                    alert('新增内容不能为空！')
                    return
                }
                //先判断是新增还是编辑
                // 发送Ajax请求
                $.ajax({
                    type: 'post',
                    url: BigNew.category_add,
                    data: {
                        name: categoryname,
                        slug: categoryslug
                    },
                    success: function (backData) {
                        if (backData.code == 201) {
                            alert('新增成功！')
                            gitData()
                            //隐藏模态框并将输入框清空
                            $('#myModal').modal('hide')
                            $('#recipient-name').val('')
                            $('#message-text').val('')
                        }
                    }
                })
            } else {
                //编辑功能
                //获取id
                // let categoryid = $('#categoryID').val().trim()
                // let categoryname = $('#recipient-name').val().tirm()
                // let categoryslug = $('#message-text').val().trim()

                let res = $('#myModal form').serialize()

                //发送Ajax请求
                $.ajax({
                    type: 'post',
                    url: BigNew.category_edit,
                    data: res,
                    success: function (backData) {
                        if (backData.code == 200) {
                            alert('编辑成功！')
                            gitData()
                            //隐藏模态框并将输入框清空
                            $('#myModal').modal('hide')
                            $('#recipient-name').val('')
                            $('#message-text').val('')
                        }
                    }
                })
            }

        })

        //点击取消清空模态框中的内容
        $('.btn-default').on('click', function () {
            // $('#recipient-name').val('')
            // $('#message-text').val('')
            $('#myModal form')[0].reset()
        })

        //点击删除删除当前的类别
        $('tbody').on('click', 'a#delete', function () {
            if (confirm('您确定要删吗？')) {
                let delId = $(this).attr('data-id')
                $.ajax({
                    type: 'post',
                    url: BigNew.category_delete,
                    data: {
                        id: delId,
                    },
                    success: function (backData) {
                        alert('删除成功！')
                        gitData()
                    }
                })
            }
        })

    })
</script>

</html>